<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\DB;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Classify extends Backend
{

  /*类别*/
  public function index()
  {
      $classify=DB::name('classify')->order('time desc')->paginate(10);
      $this->assign('classify',$classify);
      $this->assign('page',$classify->render());
      return $this->fetch('classify/index');
  }
  /*
   * 添加类别
   */
  public function add()
  {
      if($_POST)
      {
          $array=$this->request->param();
          $array['time']=time();
          $state=DB::name('classify')->insert($array);
          if($state)
          {
            return  $this->success('添加成功',url('classify/index'));
          }else{
           return   $this->error('添加失败');
          }
      }else{
          return $this->fetch('classify/add');
      }
  }
  /*
   *修改类别
   * */
  public function detail()
  {
      if($_POST)
      {
          $where['id']=$_POST['id'];
          $date['name']=$_POST['name'];
          $date['state']=$_POST['state'];
          $date['time']=time();
          $classify=DB::name('classify')->where($where)->update($date);
          return $this->success($classify,'classify/index');
      }else{
          $param = $this->request->param();
          $id=$param['id'];
          $classify=DB::name('classify')->where(array('id'=>$id))->find();
          $this->assign('classify',$classify);
          return $this->fetch('detail');
      }
  }
  /*删除*/
  public  function delete(){
      $param = $this->request->param();
      $id=$param['id'];
      DB::name('classify')->where('id',$id)->delete();
      $this->success('删除成功');
  }
}
